package dao;
import java.sql.*;
import model.*;
import java.util.*;



public class RoomDao {
	private static RoomDao roomdao=new RoomDao();
	private RoomDao() {};
	public static RoomDao getInstance() {
		return roomdao;             
	}
	
// 酒店已建好，故不考虑房间的插入删除
	public boolean rentRoom(int rid) {
		String sql="update Room set hasRented=hasRented+1 where rid="+rid;
		Connection conn=BaseConn.getConn();
		PreparedStatement pstm=null;
		try {
			pstm=conn.prepareStatement(sql);
			return pstm.executeUpdate()>0;
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally {
			
			try {
				pstm.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
		
	}
	public boolean returnRoom(int rid) {
		String sql="update Room set hasRented=hasRented-1 where rid="+rid;
		Connection conn=BaseConn.getConn();
		PreparedStatement pstm=null;
		try {
			pstm=conn.prepareStatement(sql);
			return pstm.executeUpdate()>0;
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally {
			
			try {
				pstm.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
		
	}
	public Room queryRoom(String type) {
		String sql="select * from Room where type= '"+type+"'";
		Connection conn=BaseConn.getConn();
		Room room=new Room();
		PreparedStatement pstm=null;
		ResultSet res=null;
		 try {
	            pstm = conn.prepareStatement(sql);
	            res = pstm.executeQuery(); // 执行
	            if (res.next()) {
	                room.setRid(res.getInt("rid"));
	                room.setAddress(res.getString("address"));
	                room.setCount(res.getInt("count"));	               
	                room.setType(res.getString("type"));
	                room.setPrice(res.getInt("price"));
	                room.setHasRented(res.getInt("hasRented"));
	
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	            	res.close(); 
	            	pstm.close();  
	            	conn.close();
	                
	               
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	        return room;
		
	}
	public Object[][] queryAllRoom(){
		String sql="select * from Room ";
		Connection conn=BaseConn.getConn();		
		List<Room> r=new ArrayList<Room>();
		PreparedStatement pstm=null;
		ResultSet res=null;
		 try {
	            pstm = conn.prepareStatement(sql);
	            res = pstm.executeQuery(); // 执行
	            while(res.next()) {
	            	Room room=new Room();
	                room.setRid(res.getInt("rid"));
	                room.setAddress(res.getString("address"));
	                room.setCount(res.getInt("count"));	               
	                room.setType(res.getString("type"));
	                room.setPrice(res.getInt("price"));
	                room.setHasRented(res.getInt("hasRented"));
	                r.add(room);
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	            	res.close(); 
	            	pstm.close();  
	            	conn.close();
	                
	               
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
		 Object[][] obj=new Object[r.size()][5];
		 int i=0;
		 for(Room room: r) {
			obj[i][0]=room.getRid();
		    obj[i][1]=room.getType();
			obj[i][2]=room.getAddress();
			obj[i][3]=room.getCount()-room.getHasRented();
	        obj[i][4]=room.getPrice();
	        i++;
		 }
		 return obj;
	}
	

}


























